package com.groupfour.attendancemanagementsystem.utils;

import io.jsonwebtoken.*;
import org.springframework.util.StringUtils;

import java.util.Date;

public class JwtHelper {
    private static long tokenExpiration = 24*60*60*1000;
    private static String tokenSignKey = "123456";

    //生成token字符串
    public static String createToken(String userCode, String userPassword, Integer userType) {
        String token = Jwts.builder()

                .setSubject("YYGH-USER")

                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))

                .claim("userCode", userCode)
                .claim("userPassword", userPassword)
                .claim("userType", userType)


                .signWith(SignatureAlgorithm.HS512, tokenSignKey)
                .compressWith(CompressionCodecs.GZIP)
                .compact();
        return token;
    }

    //从token字符串获取userCode
    public static String getUserCode(String token) {
        if(StringUtils.isEmpty(token)) {
            return "";
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        String userCode = (String) claims.get("userCode");
        return userCode;
    }

    //从token字符串获取userPassword
    public static String getUserPassword(String token) {
        if(StringUtils.isEmpty(token)) {
            return "";
        }
        Jws<Claims> claimsJws
                = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        return (String)claims.get("userPassword");
    }

    //从token字符串获取userType
    public static Integer getUserType(String token) {
        if(StringUtils.isEmpty(token)) {
            return null;
        }
        Jws<Claims> claimsJws
                = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        return (Integer)(claims.get("userType"));
    }


    //判断token是否有效
    public static boolean isExpiration(String token){
        try {
            boolean isExpire = Jwts.parser()
                    .setSigningKey(tokenSignKey)
                    .parseClaimsJws(token)
                    .getBody()
                    .getExpiration().before(new Date());
            //没有过期，有效，返回false
            return isExpire;
        }catch(Exception e) {
            //过期出现异常，返回true
            return true;
        }
    }


    /**
     * 刷新Token
//     * @param token
     * @return
     */
//    public String refreshToken(String token) {
//        String refreshedToken;
//        try {
//            final Claims claims = Jwts.parser()
//                    .setSigningKey(tokenSignKey)
//                    .parseClaimsJws(token)
//                    .getBody();
//            refreshedToken = JwtHelper.createToken(getUserId(token), getUserType(token));
//        } catch (Exception e) {
//            refreshedToken = null;
//        }
//        return refreshedToken;
//    }



//    public static void main(String[] args) {
//        String token = JwtHelper.createToken("199000332", "123456",0);
//        System.out.println(token);
//        System.out.println(JwtHelper.getUserCode(token));
//        System.out.println(JwtHelper.getUserPassword(token));
//        System.out.println(JwtHelper.getUserType(token));
//    }
}
